package org.example.threadorder;

import lombok.extern.slf4j.Slf4j;

/**
 * @author zhengdp
 * @Description 线程顺序控制（wait/notify版本）
 * 先输出2 在输出1
 * @Date 2022/11/9 22:18
 * @Created by zhengdp
 */
@Slf4j(topic = "c.ThreadOrderWaitNotify")
public class ThreadOrderWaitNofity {
    static final Object lock = new Object();
    // t2是否运行过
    static  boolean t2Runned = false;
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                while(!t2Runned){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                log.info("1");
            }
        },"t1");

        Thread t2 = new Thread(() -> {
            synchronized (lock) {
                log.info("2");
                t2Runned = true;
                lock.notify();
            }
        },"t2");
        t1.start();
        t2.start();
    }
}
